<?php

abstract class AbstractPager {

    protected $pattern = array("back_page" => "&#8249;&#8249;"
        , "next_page" => "&#8250;&#8250;"
        , "seperate" => " - "
        , "page_class_css" => ""
        , "current_page_class_css" => ""
        , "page_query_string" => "page"
        , "first_page" => ""
        , "last_page" => "");
    protected $total_item = 0; //Total record
    protected $item_per_page = 0; //Total record, record is shown on screen
    protected $num_page_view = 0; //Total page is shown
    protected $page = 0;
    protected $params = null;
    protected $link = "";

    public function __construct(&$rto, $link, $params, $item_per_page, $num_page_view) {
        $this->page = isset($_REQUEST[$this->pattern["page_query_string"]]) ? $_REQUEST[$this->pattern["page_query_string"]] : 1;
        $this->item_per_page = $item_per_page;
        $this->num_page_view = $num_page_view;
        $this->params = $params;
        $this->link = $link;
        if ($rto != null) {
            $start = 0;
            $end = $item_per_page;
            if ($this->page > 1) {
                $start = (($this->page - 1) * $item_per_page) ;
                $end =  $item_per_page ;
            }
            $rto->prepareLimit($start, $end);
            $this->total_item = $rto->findCount();
        }
    }

    public function getTotalItem(){
        return $this->total_item;
    }

    public function setAttribute($name, $value) {
        if (isset($this->pattern[$name])) {
            $this->pattern[$name] = $value;
        }
    }

    public function getStartPage() {
        if ($this->total_item > 0) {
            return 1;
        }
        return 0;
    }

    public function getCurrentPage() {
        return $this->page;
    }

    public function getEndPage() {
        if ($this->total_item > 0) {
            $result = $this->total_item % $this->item_per_page;
            $x = intval($this->total_item / $this->item_per_page);
            if ($result != 0) {
                return $x + 1;
            } else {
                return $x;
            }
        }
        return 0;
    }

    public function getAttributePage() {
        $attr = array();
        $index = 1;
        $current_page = 1;
        $loop = $this->num_page_view;
        $end_page = $this->getEndPage();
        for ($i = 1; $i < $end_page; $i++) {
            if ($loop == 0) {
                $current_page = $i;
                $loop = $this->num_page_view;
            }

            if ($this->page == $i) {
                $i = $end_page;
            }
            $loop--;
        }

        for ($index = $current_page, $count = 1; $count <= $this->num_page_view && $index <= $end_page; $index++, $count++) {
            $attr[] = $index;
        }

        return $attr;
    }

    public function getNextPage() {
        if ($this->page < $this->getEndPage()) {
            return $this->page + 1;
        }
        return $this->page;
    }

    public function getBackPage() {
        if ($this->page > 1) {
            return $this->page - 1;
        }
        return $this->page;
    }

    public function getQueryString() {
        $result = "";
        $index = 0;
        foreach ($this->params as $key => $value) {
            if ($index == 0) {
                $result = $key . "=" . urlencode($value);
                $index++;
            } else {
                $result = $result . "&" . $key . "=" . urlencode($value);
            }
        }
        return $result;
    }

    public abstract function display();
}
